<?php

if (!defined('BASEPATH'))
     exit('No direct script access allowed');

class Tfidf extends CI_Controller {

     function __construct() {
          parent:: __construct();
          $this->load->model('tfidf_model');
          $this->load->model('tanya_model');
     }

     /*
      * parameter jumlah dokumen: count_document
      * parameter jumlah row: count_row_token
      * parameter array kata: array_token
      */

     function array_kata($pilih) {
          $this->load->library('kelas_pemisahan');
          $data = $this->tfidf_model->data_kasus("result")->result();
          $i = 1;

          $kalimat[0] = "";
          foreach ($data as $row) {
               $kalimat[$i] = $row->kasus;
               $kalimat[$i] = $kalimat[$i - 1] . " " . $kalimat[$i];
               $i++;
          }
          $j = 0;

          foreach ($data as $row) {
               $kalimat2[$j] = $row->kasus;
               $j++;
          }
          //hahah
          $k = 0;
          foreach ($kalimat2 as $row) {
               $kata_per_kalimat[$k] = $this->kelas_pemisahan->tokenizing($row);
               $k++;
          }

          $kata = $this->kelas_pemisahan->tokenizing($kalimat[$i - 1]);
          $stoplist = $this->tanya_model->get_stopwords()->row()->stopwords;
          $data_filter = $this->kelas_pemisahan->filtering($kata, $stoplist);
          if ($pilih == "unfilter_token") {
               return $data;
          }
          $l = 0;
          foreach ($kata_per_kalimat as $row) {
               $data_filter2[$l] = $this->kelas_pemisahan->filtering($kata_per_kalimat[$l], $stoplist);
               foreach ($data_filter2 as $cols) {
                    $data_filter3[$l] = $cols;
               }
               $l++;
          }

          $unique_data_filter = array_unique($data_filter);

          $a = 0;
          //"kata_" dimulai dari 1
          foreach ($unique_data_filter as $row) {
               $kata2["kata_" . $a] = $row;
               $a++;
          }

          for ($m = 1; $m < count($kata2); $m++) {
               for ($n = 0; $n < count($data_filter3); $n++) {
                    $posisisi["kata_" . $m][$n] = count(array_keys($data_filter3[$n], $kata2["kata_" . $m]));
               }
          }
          if ($pilih == "posisi_token") {
               return $posisisi;
          } else if ($pilih == 'array_token') {
               unset($kata2["kata_0"]);
               return $kata2;
          } else if ($pilih == 'count_document') {
               return $this->tfidf_model->data_kasus("count");
          } else if ($pilih == 'count_row_token') {
               return count($kata2);
          } else {
               echo "<h2>Wrong Parameter!!</h2> " .
               "<br><b>parameter jumlah dokumen:</b> count_document. " .
               "<br><b>parameter jumlah row kata:</b> count_row_token. " .
               "<br><b>parameter array kata:</b> array_token" .
               "<br><br>ex: array_kata('count_document')";
          }
     }

     /*
      * W(d,t)=TF(d,t)*IDF(t)
      * d = dokumen ke-d
      * t = kata ke-t dari kata kunci
      * W = bobot dokumen ke-d terhadap kata kunci ke-t
      * -------------
      * Jumlah dokumen = D
      */

     public function hitung_tfidf() {
          //jumlah dokumen
          $D = $this->array_kata('count_document');

          //array token
          $array_token = $this->array_kata('array_token');
          //print_r($print);
          //meletakkan array_token ke dalam array bernama token
          $key1 = 1;
          foreach ($array_token as $row) {
               $token["kata_" . $key1] = $row;
               $key1++;
          }
          echo "token<br>";
          echo "------<br>";
          foreach ($token as $row) {
               echo $row . "<br>";
          }
          //baca kata kunci

          $key2 = 0;
          $kata_kunci = array('hihihi' => 'logistik', 'hahaha' => 'pengetahuan');
//          foreach ($kata_kunci as $kata_baris) {
//               $posisi_kk[$key2] = (array_keys($array_token, $kata_baris));
//               $posisi_kkk[$key2] = (array_keys($this->array_kata("unfilter_token"), $kata_baris));
//               $key2++;
//          }
          //jumlah kata kunci
          $a = 1;
          foreach ($array_token as $row) {
               $jumlah_kata_kunci[$a] = count(array_keys($kata_kunci, $row));
               $a++;
          }

          $posisi_dokumen = $this->array_kata("posisi_token");

          for ($a = 1; $a <= count($posisi_dokumen); $a++) {
               $sum_dokumen["kata_" . $a] = log10($D / array_sum($posisi_dokumen["kata_" . $a]));
               //bobot kata kunci
               $idf_kata_kunci["kata_".$a] = $jumlah_kata_kunci[$a]*$sum_dokumen["kata_".$a];
               for ($b = 0; $b < $D; $b++) {
                    $dokumen_w_tfidf["kata_" . $a][$b] = $posisi_dokumen["kata_" . $a][$b] * $sum_dokumen["kata_" . $a];
               }
          }

          //bobot kata kunci kuadrat
          for ($a=1; $a<= count($idf_kata_kunci); $a++){
               $idf_kata_kunci_kuadrat["kata_".$a] = $idf_kata_kunci["kata_".$a]*$idf_kata_kunci["kata_".$a];
          }
          //jumlah, dikuadratkan, lalu diakar bobot kata kuncinya
          $sqrt_sum_idf_kata_kunci_kuadrat = sqrt(array_sum($idf_kata_kunci_kuadrat));

          //normalisasi kata kunci
          for($a=1; $a<=count($idf_kata_kunci); $a++){
               $normalisasi_kata_kunci["kata_".$a] = $idf_kata_kunci["kata_".$a]/$sqrt_sum_idf_kata_kunci_kuadrat;
          }

          echo "<br>-----<br>bobot tiap kata (W) TF-IDF<br>-----";
          echo "<table>";
          foreach ($dokumen_w_tfidf as $row) {
               echo "<tr>\n";
               foreach ($row as $col) {
                    echo "<td>";
                    echo $col;
                    echo "</td>\n";
               }
               echo "</tr>\n";
          }
          echo "</table>";

          for ($a = 1; $a <= count($posisi_dokumen); $a++) {
               for ($b = 0; $b < $D; $b++) {
                    $dokumen_w_tfidf_kuadrat["kata_" . $a][$b] = $dokumen_w_tfidf["kata_" . $a][$b] * $dokumen_w_tfidf["kata_" . $a][$b];
               }
          }

          //transpose tabel bobot tfidf kuadrat
          $trans_w_tfidf_kuadrat = $this->transpose_array($dokumen_w_tfidf_kuadrat);

          //jumlah dan diakar bobot TF-IDF di tiap dokumen
          for ($a = 0; $a < count($trans_w_tfidf_kuadrat); $a++) {
               $sqrt_sum_w_tfidf_kuadrat[$a] = sqrt(array_sum($trans_w_tfidf_kuadrat[$a]));
          }

          //normalisasi dokumen
          for ($a = 1; $a <= count($posisi_dokumen); $a++) {
               for ($b = 0; $b < $D; $b++) {
                    $normalisasi["kata_" . $a][$b] = $dokumen_w_tfidf["kata_" . $a][$b] / $sqrt_sum_w_tfidf_kuadrat[$b];
               }
          }

          //normalisasi kata kunci dikalikan normalisasi dokumen
          for ($a=1; $a<=count($posisi_dokumen); $a++){
               for ($b=0; $b<$D; $b++){
                    $nor_kunci_dokumen["kata_".$a][$b] = $normalisasi["kata_".$a][$b]*$normalisasi_kata_kunci["kata_".$a];
               }
          }

          echo "<br>-----<br>Normalisasi TF-IDF tiap kata/dokumen<br>-----";
          echo "<table>";
          foreach ($normalisasi as $row) {
               echo "<tr>\n";
               foreach ($row as $col) {
                    echo "<td>";
                    echo $col;
                    echo "</td>\n";
               }
               echo "</tr>\n";
          }
          echo "</table>";
          
          //transpose nor kunci dokumen
          $trans_nor_kunci_dokumen=$this->transpose_array($nor_kunci_dokumen);

          //sum perkalian kata kunci dan dokumen
          for ($a=0; $a<$D; $a++){
          $sum_trans_nor_kunci_dokumen["Dokumen_".$a] = array_sum($trans_nor_kunci_dokumen[$a]);
          }

          //normalisasi kata kunci dikuadratkan
          for ($a=1; $a<=count($normalisasi_kata_kunci); $a++){
               $normalisasi_kata_kunci_kuadrat["kata_".$a] = $normalisasi_kata_kunci["kata_".$a]*$normalisasi_kata_kunci["kata_".$a];
          }

          //normalisasi kata kunci yang dikuadratkan kemudian diakar (nanti akan dikalikan dengan hasil akar kuadrat normalisasi dari tiap dokumen
          for ($a=1; $a<=count($normalisasi_kata_kunci); $a++){
               $sqrt_normalisasi_kata_kunci_kuadrat = sqrt(array_sum($normalisasi_kata_kunci_kuadrat));
          }

          //menguadratkan masing2 kata normalisasi di tiap dokumen
          for ($a=1; $a<=count($posisi_dokumen); $a++){
               for ($b=0; $b<$D; $b++){
                    $normalisasi_kuadrat["kata_".$a][$b] = $normalisasi["kata_".$a][$b]*$normalisasi["kata_".$a][$b];
               }
          }

          //transpose normalisasi yang telah dikuadratkan
          $trans_normalisasi_kuadrat = $this->transpose_array($normalisasi_kuadrat);

          //menjumlahkan, kemudian mengakar hasil kuadrat masing2 kata normalisasi di tiap dokumen (dokumen 0, 1, 2, dst...)
          for ($a=0; $a<count($trans_normalisasi_kuadrat); $a++){
               $sqrt_sum_trans_normalisasi_kuadrat["Dokumen_".$a] = sqrt(array_sum($trans_normalisasi_kuadrat[$a]));
          }

          for($a=0; $a<count($sqrt_sum_trans_normalisasi_kuadrat); $a++){
               $cosine["Dokumen_".$a] = $sum_trans_nor_kunci_dokumen["Dokumen_".$a]/($sqrt_normalisasi_kata_kunci_kuadrat*$sqrt_sum_trans_normalisasi_kuadrat["Dokumen_".$a]);
          }

          print_r($cosine);
          
     }

     function transpose_array($array) {
          $output_transpose = array();
          foreach ($array as $key => $subarr) {
               foreach ($subarr as $subkey => $subvalue) {
                    $output_transpose[$subkey][$key] = $subvalue;
               }
          }
          return $output_transpose;
     }
}